#include "CCamera.h"
#include "..\CGame.h"
#include "CPlayer.h"
CCamera* CCamera::instance = NULL;

CCamera::CCamera(void)
{
	m_fScreenX = 0.0f;
	m_fScreenY = 0.0f;
	m_nOffsetX = 0;
	m_nOffsetY = 0;
	m_nScreenWidth = CGame::GetInstance()->GetScreenWidth();
	m_nScreenHeight = CGame::GetInstance()->GetScreenHeight();
	m_pTarget = CPlayer::GetInstance();

	// Set the rects for the camera
	m_rInnerRect.left = long(m_fScreenX + (m_nScreenWidth*0.3f));
	m_rInnerRect.top = long(m_fScreenY + (m_nScreenHeight*0.3f));
	m_rInnerRect.right = long(m_fScreenX + (m_nScreenWidth*0.7f));
	m_rInnerRect.bottom = long(m_fScreenY + (m_nScreenHeight*0.7f));

	m_rOuterRect.left = long(m_fScreenX);
	m_rOuterRect.top = long(m_fScreenY);
	m_rOuterRect.right = long(m_fScreenX + m_nScreenWidth);
	m_rOuterRect.bottom = long(m_fScreenY + m_nScreenHeight);
}

CCamera::~CCamera(void)
{
	m_pTarget = NULL;
}

CCamera* CCamera::GetInstance()
{
	static CCamera instance;

	return &instance;
}

RECT CCamera::GetCameraRect()
{
	//RECT rCamera;
	//rCamera.left = long(m_fScreenX); 
	//rCamera.top = long(m_fScreenY);
	//rCamera.right = rCamera.left + long(m_nScreenWidth);
	//rCamera.bottom = rCamera.top + long(m_nScreenHeight);
	//return rCamera;
#if 0
	RECT testRect = m_rOuterRect;
	testRect.top += 0.25f * GetScreenHeight();
	testRect.bottom -= 0.25f * GetScreenHeight();
	testRect.right -= 0.25f * GetScreenWidth();
	testRect.left += 0.25f * GetScreenWidth();
	return testRect;

#endif

	return m_rOuterRect;
}

void CCamera::Update(CBase* pTarget)
{
	if(!pTarget) // error checking
		return;


	

	
	///////////////////////////////////////////
	// Set the camera's position relative to the player's position

	if((pTarget->GetPosX() + pTarget->GetWidth()) > m_rInnerRect.right)
	{
		int diff = int(((pTarget->GetPosX() + pTarget->GetWidth()) - m_rInnerRect.right));
		m_nOffsetX += diff;
		//m_pTarget->SetPosX(m_pTarget->GetPosX() - diff);
		m_rInnerRect.right += diff;
		m_rInnerRect.left += diff;
	}
	if(pTarget->GetPosX() < m_rInnerRect.left)
	{
		int diff = int((m_rInnerRect.left - pTarget->GetPosX()));
		m_nOffsetX -= diff;
		//m_pTarget->SetPosX(m_pTarget->GetPosX() + diff);
		m_rInnerRect.right -= diff;
		m_rInnerRect.left -= diff;
	}
	if((pTarget->GetPosY() + pTarget->GetHeight()) > m_rInnerRect.bottom)
	{
		int diff = int(((pTarget->GetPosY() + pTarget->GetHeight()) - m_rInnerRect.bottom));
		m_nOffsetY += diff;
		//m_pTarget->SetPosY(m_pTarget->GetPosY() - diff);
		m_rInnerRect.top += diff;
		m_rInnerRect.bottom += diff;
	}
	if(pTarget->GetPosY() < m_rInnerRect.top)
	{
		int diff = int((m_rInnerRect.top - pTarget->GetPosY()));
		m_nOffsetY -= diff;
		//m_pTarget->SetPosY(m_pTarget->GetPosY() + diff);
		m_rInnerRect.top -= diff;
		m_rInnerRect.bottom -= diff;
	}
	///////////////////////////////////////////

}

void CCamera::DeleteInstance(void)
{
	if(instance != 0)
	{
		delete instance; 
		instance = NULL;
	}
}